﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eBay.Service.Core.Soap;

namespace SalePromotion.Extensions
{

    public static class SelectListHelper
    {
        private const string OptionItem = "<option value=\"{0}\" {1}>{2}</option>";

       public static IEnumerable<SelectListItem> GetHourOrMinutes(this int upperLimit)
       {
           var array = new List<string>();
           for (int i = 0; i < upperLimit; i++)
           {
               array.Add(i.ToString().PadLeft(2,'0'));
           }
           return array.ToSelectListItems(false);
       }

         public static IEnumerable<SelectListItem> GetSortBy()
       {
           return new[] { ItemSortTypeCodeType.EndTime.ToString(), ItemSortTypeCodeType.CurrentPriceDescending.ToString(), ItemSortTypeCodeType.CurrentPrice.ToString(), ItemSortTypeCodeType.StartTime.ToString() }.ToSelectListItems(false);
          
          
       }
         public static IEnumerable<SelectListItem> GetListingFormat()
         {

             return new[] { ListingTypeCodeType.FixedPriceItem.ToString(), ListingTypeCodeType.Auction.ToString() }.ToSelectListItems(false);
         }



        public static string ToSelectListOptions(this IEnumerable<string> values, string selectedValue, string emptyOptionText = "")
        {
            var result = values.Select(i => new KeyValuePair<string, string>(i, i)).ToSelectListOptions(selectedValue, emptyOptionText);
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="values">The key of this pair represents the value displayed in the option list, the value of the pair represents the id of the option tag</param>
        /// <param name="selectedValue"></param>
        /// <param name="emptyOptionText"></param>
        /// <param name="ifOnlyOneItemSelectIt"></param>
        /// <returns></returns>
        public static string ToSelectListOptions(this IEnumerable<KeyValuePair<string, string>> values, string selectedValue = "", string emptyOptionText = "", bool ifOnlyOneItemSelectIt = false)
        {
            var items = values.Select(i =>
                String.Format(OptionItem,
                    i.Value,
                    (i.Value == selectedValue || (ifOnlyOneItemSelectIt && values.Count()==1)) ? "selected=\"selected\"" : string.Empty,
                    i.Key
                )
            );

            return
                String.Format(
                    "<option>{0}</option>{1}",
                    emptyOptionText,
                    String.Join(Environment.NewLine, items)
                );
        }

        public static IEnumerable<SelectListItem> ToSelectListItems(this StoreCustomCategoryTypeCollection model, string emptyOptionText = "", bool ifOnlyOneItemSelectIt = false)
         {
             if (model == null)
                 return new SelectListItem[0];

             var items = model.ToArray().Select(i => new SelectListItem { Text = i.Name, Value = i.CategoryID.ToString(), Selected = (ifOnlyOneItemSelectIt && model.Count==1) });
             var text = string.IsNullOrWhiteSpace(emptyOptionText) ? string.Empty : emptyOptionText + ":";
             return (new[] { new SelectListItem { Text = text, Value = string.Empty } }).Concat(items);
         }


        private static IEnumerable<SelectListItem> ToSelectListItems(this Type type, string emptyOptionText = "", bool withEmptyOption = true)
        {
            var values = type.GetFields()
                        .Select(i => new SelectListItem { Value = Convert.ToString(i.GetValue(null)), Text = i.Name })
                        .OrderBy(i => i.Text);

            if (!withEmptyOption)
                return values;

            var text = string.IsNullOrWhiteSpace(emptyOptionText) ? string.Empty : emptyOptionText + " :";
            return (new[] { new SelectListItem { Text = text, Value = string.Empty } }).Concat(values);
        }

        private static IEnumerable<SelectListItem> ToSelectListItems(this IEnumerable<string> values, bool withEmptyOption = true)
        {
            if(withEmptyOption)
            return
                 new[] { string.Empty }.Concat(values)
               .OrderBy(i => i)
               .Select(i => new SelectListItem { Text = i });
            return 
                  values
               .OrderBy(i => i)
               .Select(i => new SelectListItem { Text = i });
        }

        public static IEnumerable<SelectListItem> EnumToList(Type values, bool withEmptyOption = true)
        {
            var a = Enum.GetNames(values);
            return
               (withEmptyOption ? new[] { string.Empty }.Concat(a) : a)
               .OrderBy(i => i)
               .Select(i => new SelectListItem { Text = i });
        }

    
    }
}